######################################################
# REPLICATION FILE FOR
# "Sustaining Capitalism and Democracy: Lessons from 
#     Global Competition Policy"
# By Michael O Allen and Kenneth Scheve
#
# IN THIS FILE:
#   Construct Plots for Figure 1
######################################################

library(data.table)
library(ggplot2)
library(cowplot)


# Set filepaths -----------------------------------------------------------

# CCL - CLI
filepath_cli <- '/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/CCL_Original/CCL_CLI_Data_Ver2.dta'

# CCL - Enforcement Data
filepath_ccl_agency <- '/Users/moda/Dropbox/Work/Yale/AntiTrust/Data/CCL_Original/CCL_Enforcement_Data_Ver1.dta'

# Polity v5
filepath_polityv5 <- '/Users/moda/Dropbox/Data/Governance/Polity5/p5v2018.xls'

# Import CCL --------------------------------------------------------------

cli <- setDT(haven::read_dta(filepath_cli))
cli <- cli[regional_group == 0, ]

ccl_agency <- setDT(haven::read_dta(filepath_ccl_agency))
ccl_agency <- ccl_agency[regional_group == 0, ]

# Import Polity -----------------------------------------------------------

polity <- setDT(readxl::read_xls(filepath_polityv5))

# Merge -------------------------------------------------------------------

plotData_cli <- merge(cli, polity[ , .(code_cowc = scode, year, polity2)],
                      by = c('code_cowc', 'year'),
                      all.x = T)

plotData_agency <- merge(ccl_agency, polity[ , .(code_cowc = scode, year, polity2)],
                         by = c('code_cowc', 'year'),
                         all.x = T)

plotData_cli$dem <- ifelse(plotData_cli$polity2 > 6, 'Democracy', 'Non-democracy')
plotData_agency$dem <- ifelse(plotData_agency$polity2 > 6, 'Democracy', 'Non-democracy')

# Legislation -------------------------------------------------------------

ggplot(cli[, .(leg_per = sum(competition_law_coded_total)), keyby = year],
       aes(x = year, y = leg_per)) +
  geom_bar(stat = 'identity', width = .7, fill = '#007fcf') +
  scale_x_continuous(breaks = seq(1890, 2010, 20)) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.03)),
                     breaks = seq(0,40, 5)) +
  #geom_smooth(method = 'loess', span = .4, se = FALSE) +
  labs(x = '', y = '# of Laws') +
  theme_cowplot() +
  background_grid(major = 'y',
                  minor = 'y')

ggsave("Plots/legislation.pdf", 
       height = 4, width = 6, units = "in")

# Mean CLI ----------------------------------------------------------------

ggplot(plotData_cli[!is.na(dem), .(cli_mean = mean(cli_overall_norm)), keyby = .(year, dem)],
       aes(x = year, y = cli_mean, color = dem)) +
  geom_line(size = .75) +
  annotate('text', x = 1963, y = .375, hjust = 0, size = 4.25, color = '#007fcf',
           label = 'Democracies') +
  annotate('text', x = 1953, y = .125, hjust = 0, size = 4.25, color = 'tomato2',
           label = 'Non-democracies') +
  scale_color_manual(values = c('#007fcf', 'tomato2')) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.03))) +
  scale_x_continuous(breaks = seq(1890, 2010, 20)) +
  labs(x = '', y = 'Mean CLI') +
  theme_cowplot() +
  background_grid(major = 'y',
                  minor = 'y') +
  theme(legend.position = 'none')

ggsave("Plots/mean_cli.pdf", 
       height = 4, width = 6, units = "in")

# Agency ------------------------------------------------------------------

plotData_agency <- plotData_agency[!is.na(dem), .(agency = mean(agency)), by = .(year, dem)]

ggplot(plotData_agency[year >= 1890],
       aes(x = year, y = agency, color = dem)) +
  geom_line(size = .75) +
  labs(y = 'Countries with Competition Agencies', x = '') +
  annotate('text', x = 1965, y = .55, hjust = 0, size = 4.25, color = '#007fcf',
           label = 'Democracies') +
  annotate('text', x = 1955, y = .13, hjust = 0, size = 4.25, color = 'tomato2',
           label = 'Non-democracies') +
  scale_color_manual(values = c('#007fcf', 'tomato2')) +
  scale_x_continuous(breaks = seq(1890, 2010, 20)) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.03)),
                     labels = scales::percent_format(1L)) +
  coord_cartesian(clip = 'off') +
  theme_cowplot() +
  background_grid(major = 'y', minor = 'y') +
  theme(legend.position = 'none')

ggsave("Plots/enforcement_agencies.pdf", 
       height = 4, width = 6, units = "in")
